/**
 * 定义页面对应的后台处理类完整路径。
 */
var clsUrl = basePath + 'JarpListener?className=cn.com.zsc.hr.AttendanceManager';

Ext.onReady(function() {
	Ext.QuickTips.init();
			
	/**
	 * 日期时间控件
	 */
	var searchDate = new Ext.form.DateField({
		id:'searchDate',
        fieldLabel:"考勤时间",
        name:'searchDate',
        format:'Y-m',
        value:new Date(),
        editable:false,
        width:90
    });
    
    var storereader= new Ext.data.JsonReader({
    	fields:[
			{name :'guideName'},			
    		{name :'day_1'},
    		{name :'day_2'},
    		{name :'day_3'},
    		{name :'day_4'},
    		{name :'day_5'},
    		{name :'day_6'},
    		{name :'day_7'},
    		{name :'day_8'},
    		{name :'day_9'},
    		{name :'day_10'},
    		{name :'day_11'},
    		{name :'day_12'},
    		{name :'day_13'},
    		{name :'day_14'},
    		{name :'day_15'},
    		{name :'day_16'},
    		{name :'day_17'},
    		{name :'day_18'},
    		{name :'day_19'},
    		{name :'day_20'},
    		{name :'day_21'},
    		{name :'day_22'},
    		{name :'day_23'},
    		{name :'day_24'},
    		{name :'day_25'},
    		{name :'day_26'},
    		{name :'day_27'},
    		{name :'day_28'},
    		{name :'day_29'},
    		{name :'day_30'},
    		{name :'day_31'},
    		{name :'attendance'},
    		{name :'holidays'},
    		{name :'attendanceRate'},
    		{name :'monthDays'}
    	]
	});
		
	/**
	 * 查询员工月考勤数据
	 */
	var store = new Ext.data.Store({
		reader:storereader,
		proxy:new Ext.data.HttpProxy({
			url:clsUrl+'&methodName=getMonthAttendance&&paramCount=1'
		}),
		listeners:{'load':function(s, rs, o) {
				for (var i=0; i<rs.length; i++) {
					if(rs[i].data.monthDays == 28) {
						Ext.getCmp("basegrid").getColumnModel().setHidden(30, true);
						Ext.getCmp("basegrid").getColumnModel().setHidden(31, true);
						Ext.getCmp("basegrid").getColumnModel().setHidden(32, true);
					} else if(rs[i].data.monthDays == 29) {
						Ext.getCmp("basegrid").getColumnModel().setHidden(31, true);
						Ext.getCmp("basegrid").getColumnModel().setHidden(32, true);
					} else if(rs[i].data.monthDays == 30) {
						Ext.getCmp("basegrid").getColumnModel().setHidden(32, true);
					}
				}
			}
		}
	});
	
	var searchPanel = new Ext.Panel({
		id:"searchPanel",
		region:"north",
		border:false,
		layout : 'column',
		labelAlign : 'right',
		height:40,
		frame:true,
		labelWidth : 80,
		items:[{
			layout : 'form',
		  	labelWidth : 60,
		  	labelAlign : 'right',
		  	columnWidth : .20,
		  	items:[searchDate]
		},{
			layout : 'form',
		  	labelWidth : 0,
		  	labelAlign : 'right',
		  	columnWidth : .30,
		  	items:[{
	  			layout : 'column',
				labelAlign : 'right',
				items:[{
					layout : 'form',
				  	labelWidth : 10,
				  	labelAlign : 'right',
				  	columnWidth : .33,
				  	items:[
				  		{xtype:'button',width:70,text:'查询',handler:search},
				  		{height:4}
				  	]
				},{
					layout : 'form',
				  	labelWidth : 10,
				  	labelAlign : 'right',
				  	columnWidth : .33,
				  	items:[
				  		{xtype:'button',width:70,text:'重置',handler:function() {
				  				Ext.getCmp("searchDate").setValue(new Date());
							}
						}
				    ]
				},{
					layout : 'form',
				  	labelWidth : 10,
				  	labelAlign : 'right',
				  	columnWidth : .34,
				  	items:[
				  		{xtype:'button',width:70,text:'导出Excel',handler:exportExcel}
				  	]
				}]
			}]
		}],
		keys: {
            key: 13,
            fn: search
        }
	});
	
	
	var basegrid = new Ext.grid.EditorGridPanel( {
		id :'basegrid',
		store :store,
		split :true,
		bodyStyle:'5px 15px 0',
		region :'center',
		border :false,
		enableColumnMove:false,   
		enableHdMenu:false,
		frame :false,
		autoScroll :true,
    	columns: [
     	            new Ext.grid.RowNumberer(),
     	            {header:'姓名',width:60,sortable:true,hidden:false,enableColumnMove:false,enableColumnResize :false, dataIndex: 'guideName'},
    	          	{header:'1',width:25,sortable:true,hidden:false,enableColumnMove:false,enableColumnResize :false, dataIndex: 'day_1'},
    	          	{header:'2',width:25,sortable:true,hidden:false,enableColumnMove:false,enableColumnResize :false, dataIndex: 'day_2'},
    	          	{header:'3',width:25,sortable:true,hidden:false,enableColumnMove:false,enableColumnResize :false, dataIndex: 'day_3'},
    	          	{header:'4',width:25,sortable:true,hidden:false,enableColumnMove:false,enableColumnResize :false, dataIndex: 'day_4'},
    	          	{header:'5',width:25,sortable:true,hidden:false,enableColumnMove:false,enableColumnResize :false, dataIndex: 'day_5'},
    	          	{header:'6',width:25,sortable:true,hidden:false,enableColumnMove:false,enableColumnResize :false, dataIndex: 'day_6'},
    	          	{header:'7',width:25,sortable:true,hidden:false,enableColumnMove:false,enableColumnResize :false, dataIndex: 'day_7'},
    	          	{header:'8',width:25,sortable:true,hidden:false,enableColumnMove:false,enableColumnResize :false, dataIndex: 'day_8'},
    	          	{header:'9',width:25,sortable:true,hidden:false,enableColumnMove:false,enableColumnResize :false, dataIndex: 'day_9'},
    	          	{header:'10',width:25,sortable:true,hidden:false,enableColumnMove:false,enableColumnResize :false, dataIndex: 'day_10'},
    	          	{header:'11',width:25,sortable:true,hidden:false,enableColumnMove:false,enableColumnResize :false, dataIndex: 'day_11'},
    	          	{header:'12',width:25,sortable:true,hidden:false,enableColumnMove:false,enableColumnResize :false, dataIndex: 'day_12'},
    	          	{header:'13',width:25,sortable:true,hidden:false,enableColumnMove:false,enableColumnResize :false, dataIndex: 'day_13'},
    	          	{header:'14',width:25,sortable:true,hidden:false,enableColumnMove:false,enableColumnResize :false, dataIndex: 'day_14'},
    	          	{header:'15',width:25,sortable:true,hidden:false,enableColumnMove:false,enableColumnResize :false, dataIndex: 'day_15'},
    	          	{header:'16',width:25,sortable:true,hidden:false,enableColumnMove:false,enableColumnResize :false, dataIndex: 'day_16'},
    	          	{header:'17',width:25,sortable:true,hidden:false,enableColumnMove:false,enableColumnResize :false, dataIndex: 'day_17'},
    	          	{header:'18',width:25,sortable:true,hidden:false,enableColumnMove:false,enableColumnResize :false, dataIndex: 'day_18'},
    	          	{header:'19',width:25,sortable:true,hidden:false,enableColumnMove:false,enableColumnResize :false, dataIndex: 'day_19'},
    	          	{header:'20',width:25,sortable:true,hidden:false,enableColumnMove:false,enableColumnResize :false, dataIndex: 'day_20'},
    	          	{header:'21',width:25,sortable:true,hidden:false,enableColumnMove:false,enableColumnResize :false, dataIndex: 'day_21'},
    	          	{header:'22',width:25,sortable:true,hidden:false,enableColumnMove:false,enableColumnResize :false, dataIndex: 'day_22'},
    	          	{header:'23',width:25,sortable:true,hidden:false,enableColumnMove:false,enableColumnResize :false, dataIndex: 'day_23'},
    	          	{header:'24',width:25,sortable:true,hidden:false,enableColumnMove:false,enableColumnResize :false, dataIndex: 'day_24'},
    	          	{header:'25',width:25,sortable:true,hidden:false,enableColumnMove:false,enableColumnResize :false, dataIndex: 'day_25'},
    	          	{header:'26',width:25,sortable:true,hidden:false,enableColumnMove:false,enableColumnResize :false, dataIndex: 'day_26'},
    	          	{header:'27',width:25,sortable:true,hidden:false,enableColumnMove:false,enableColumnResize :false, dataIndex: 'day_27'},
    	          	{header:'28',width:25,sortable:true,hidden:false,enableColumnMove:false,enableColumnResize :false, dataIndex: 'day_28'},
    	          	{header:'29',width:25,sortable:true,hidden:false,enableColumnMove:false,enableColumnResize :false, dataIndex: 'day_29'},
    	          	{header:'30',width:25,sortable:true,hidden:false,enableColumnMove:false,enableColumnResize :false, dataIndex: 'day_30'},
    	          	{header:'31',width:25,sortable:true,hidden:false,enableColumnMove:false,enableColumnResize :false, dataIndex: 'day_31'},
    	          	{header:'出勤日',width:50,sortable:true,hidden:false,enableColumnMove:false,enableColumnResize :false, dataIndex: 'attendance'},
    	          	{header:'公休日',width:50,sortable:true,hidden:false,enableColumnMove:false,enableColumnResize :false, dataIndex: 'holidays'},
    	          	{header:'出勤率',width:50,sortable:true,hidden:false,enableColumnMove:false,enableColumnResize :false, dataIndex: 'attendanceRate'}
    	]
	});
	
	var mainPanel = new Ext.Panel({
            id: 'mainPanel',
            region:'center',
            split:true,
            border:false,
            frame:true,
            layout: 'border',  
            items:[searchPanel,basegrid]
    });
    
	var viewport = new Ext.Viewport( {
		layout:'border',
		items: [mainPanel]
	});

    function search(){
		mainPanel.body.mask('正在加载数据...','x-mask-loading');
		var searchDate = Ext.getCmp("searchDate").getRawValue();
		
    	var url=clsUrl+'&methodName=getMonthAttendance&paramCount=0'
		Ext.http.send(url,function(res){
			basegrid.store.loadData(Ext.decode(res));
			mainPanel.body.unmask();
		},searchDate);
	}
	
	function exportExcel() {
		var vExportContent = basegrid.getExcelXml(); // 获取数据
		if (Ext.isIE6 || Ext.isIE7 || Ext.isIE8 || Ext.isSafari || Ext.isSafari2 || Ext.isSafari3) { // 判断浏览器
			var fd = Ext.get('frmDummy');
			if (!fd) {
				fd = Ext.DomHelper.append(Ext.getBody(), {
							tag : 'form',
							method : 'post',
							id : 'frmDummy',
							action : exportUrl,
							target : '_blank',
							name : 'frmDummy',
							cls : 'x-hidden',
							cn : [{
										tag : 'input',
										name : 'exportContent',
										id : 'exportContent',
										type : 'hidden'
									}]
						}, true);
			}
			fd.child('#exportContent').set({
						value : vExportContent
					});
			fd.dom.submit();
		} else {
			document.location = 'data:application/vnd.ms-excel;base64,'+ Base64.encode(vExportContent);
		}
	}
	
	store.on('beforeload',function(thiz,options){
		mainPanel.body.mask('正在加载数据','x-mask-loading');
    });
    
	store.on("load",function(){
		mainPanel.body.unmask();
	});
	
	store.load({
        params: {
            param_0: Ext.getCmp("searchDate").getRawValue()
        }
    });
});